package com.knoya.chejian.pda.module.zupan.pan

import android.app.Activity
import android.app.ActivityManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.text.Editable
import android.util.Log
import android.view.View
import com.knoya.chejian.pda.R
import com.knoya.chejian.pda.activity.BaseActivity
import com.knoya.chejian.pda.myadapter.ArrayAdapter
import com.knoya.chejian.pda.util.Myutil
import kotlinx.android.synthetic.main.activity_pan.*
import kotlinx.android.synthetic.main.header_knoya.*

class PanActivity : BaseActivity(), View.OnClickListener {


//    var gaodu = ""
//    var weizhi = ""
    var pallet_num = ""
//    val gaodus = ArrayList<String>()
    override fun getLayoutId(): Int {
        return R.layout.activity_pan
    }

    override fun initCreate() {
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text="设置托盘"
//        gaodu = intent.getStringExtra("gaodu")
//        weizhi = intent.getStringExtra("weizhi")
        pallet_num = intent.getStringExtra("pallet_num")

//        gaodus.add("800mm")
//        gaodus.add("600mm")
//        val adapter = ArrayAdapter(this, R.layout.lv_sp_tvitem, gaodus)
//        Sp_Gaodu.adapter = adapter
//        Bt_Queren.setOnClickListener(this)
        Bt_YiPan.setOnClickListener(this)
        Et_Pallet_num.setText(pallet_num)
//        Et_Weizhi.setText(weizhi)
//        if(gaodu == "800"){
//            Sp_Gaodu.setSelection(0)
//        }else if(gaodu == "600"){
//            Sp_Gaodu.setSelection(1)
//        }

        mBoradcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if(!isAPPBroughtToBackground()){
                    return
                }
                if (intent.action == "nlscan.action.SCANNER_RESULT" && intent.hasExtra("SCAN_BARCODE1")) {
                    val scanResult_1 = intent.getStringExtra("SCAN_BARCODE1").trim()
                    samiaoOk(scanResult_1)
                }
                if(intent.action == "android.intent.ACTION_DECODE_DATA" && intent.hasExtra("barcode_string")){
                    val scanResult = intent.getStringExtra("barcode_string").trim()
                    samiaoOk(scanResult)
                }
            }
        }
        Et_Pallet_num.setOnEditorActionListener { _, actionId, _ ->
            var isOK = true
            isOK
        }
//        Et_Weizhi.setOnEditorActionListener { _, actionId, _ ->
//            var isOK = true
//            isOK
//        }
    }

    //扫描获取到数据后处理
    fun samiaoOk(data : String){
        var str=data.substring(0,2)
        if(str == "TP" || str == "AP"){
            Et_Pallet_num.setText(data)
        }else{
            Myutil.MyTost("扫描错误，无法识别")
//            Et_Weizhi.setText(data)
        }
    }

    override fun onClick(v: View?) {
        when(v){
//            Bt_Queren -> {
//                val intent = Intent()
//                if(Sp_Gaodu.selectedItem.toString() == "800mm"){
//                    intent.putExtra("gaodu", "800")
//                }else if(Sp_Gaodu.selectedItem.toString() == "600mm"){
//                    intent.putExtra("gaodu", "600")
//                }else{
//                    intent.putExtra("gaodu", "0")
//                }
//                intent.putExtra("weizhi", Et_Weizhi.text.toString())
//                intent.putExtra("pallet_num", Et_Pallet_num.text.toString())
//                setResult(Activity.RESULT_OK, intent)
//                finish()
//            }
            iv_header_left -> {
                finish()
            }
            Bt_YiPan -> {
                if(Et_Pallet_num.text.toString().trim() == ""){
                    Myutil.tishidialog(this@PanActivity,"输入托盘后才能移盘")
                    return
                }
                val intent = Intent(this, YiPanActivity::class.java)
                intent.putExtra("pallet_num", Et_Pallet_num.text.toString().trim())
                startActivityForResult(intent,50)
            }
        }
    }



    fun isAPPBroughtToBackground(): Boolean {
        val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val tasks = am.getRunningTasks(1)
        if (!tasks.isEmpty()) {
            val topActivity = tasks[0].topActivity
            if (topActivity.className == this.javaClass.name) {

                return true
            }
        }
        return false
    }

}
